package com.example.juc.demobase;


import java.util.concurrent.locks.ReentrantLock;

class TicketLock{

    private int numlber=1000;

    //创建可重入锁
    private final ReentrantLock lock=new ReentrantLock();
    public void sale(){
        lock.lock();
        try {
            if(numlber>0){
                numlber--;
                System.out.println(Thread.currentThread().getName()+" sold "+numlber);
            }
        }catch (Exception e){
            lock.unlock();
        }
    }

}


public class SaleTicketLock {
    public static void main(String[] args) {
        TicketLock ticket=new TicketLock();
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                for(int i=0;i<800;i++){
                    ticket.sale();
                }
            }
        }, "Thread1");
        thread.start();

        new Thread(()->{
            for(int i=0;i<1000;i++){
                ticket.sale();
            }
        }, "Thread2").start();



    }

}
